<!DOCTYPE html>
<html lang="en" xmlns="http://www.w3.org/1999/xhtml" xmlns:th="http://www.thymeleaf.org">
<head>
    <meta charset="utf-8"/>
    <link rel="stylesheet" th:href="@{/layui/css/layui.css}">
    <link rel="stylesheet" th:href="@{/css/bootstrap.min.css}">
    <link rel="stylesheet" th:href="@{/semantic/semantic.min.css}"/>
    <link rel="stylesheet" href="https://cdn.jsdelivr.net/npm/font-awesome/css/font-awesome.min.css">
</head>
<div style="margin:50px 50px 50px 50px">
    <form class="layui-form layui-form-pane" lay-filter="optionInfo">
        <div class="layui-form-item">

            <label class="layui-form-label"><i class="ui blue book icon"></i>项目编号</label>
            <div class="layui-input-block">
                <input type="text" name="projectId" required lay-verify="required" autocomplete="off"
                       placeholder="请输入项目名称" class="layui-input">
            </div>
        </div>

        <div class="layui-form-item">
            <label class="layui-form-label"><i class="ui violet user outline icon"></i>提出人ID</label>
            <div class="layui-input-block">
                <input type="text" name="staffId" required lay-verify="exits" class="layui-input">
                <input type="hidden" name="optionId" class="layui-input">
            </div>
        </div>

        <div class="layui-form-item layui-form-text">
            <label class="layui-form-label"><i class="ui violet user outline icon"></i>意见内容</label>
            <div class="layui-input-block">
                <textarea name="content" required lay-verify="required" class="layui-textarea"></textarea>
            </div>
        </div>

        <div class="layui-form-item" style="margin-left: -110px">
            <div class="layui-input-block">
                <button class="layui-btn" lay-submit="" lay-filter="update">更新</button>
                <button type="reset" class="layui-btn layui-btn-danger">重置</button>
            </div>
        </div>
    </form>
</div>
<script type="text/javascript" th:src="@{/js/jquery-3.4.1.min.js}"></script>
<script type="text/javascript" th:src="@{/semantic/semantic.min.js}"></script>
<script type="text/javascript" th:src="@{/layui/layui.js}"></script>
<script type="text/javascript" th:src="@{/js/upload.js}"></script>
<script th:inline="javascript">
    function isExist() {
        let flag = false;
        $.ajax({
            url: "/client/select",
            method: "post",
            contentType: "application/json;charset=utf-8",
            data: JSON.stringify({"clientId": Number($("#clientId").val())}),
            dataType: "json",
            async: false,
            success: function (res) {
                if (res.result.length !== 0) {
                    flag = true;
                }
            }
        })

        return flag;
    }

    layui.use(['form', 'layedit', 'laydate'], function () {
        const form = layui.form
            , layer = layui.layer
        //监听提交
        form.verify({
            exits: function () {
                if (!isExist()) {
                    return "该客户不存在"
                }
            }
        });
        const option = [[${option}]];//得到model中的client
        form.val('optionInfo', {
            "projectId": option.projectId
            , "staffId": option.staffId // "name": "value"
            , "content": option.content
            , "optionId": option.optionId
        });
        form.on('submit(update)', function (data) {
            $.ajax({
                url: "/option/update",
                method: "post",
                contentType: "application/json;charset=utf-8",
                data: JSON.stringify(data.field),
                dataType: "json",
                success: function (res) {
                    if (res.msg === 'FAIL') {
                        layer.alert(res.result, {
                            icon: 2,
                            skin: 'layer-ext-moon'
                        })
                    } else {
                        layer.alert('修改成功', {
                            icon: 1,
                            skin: 'layer-ext-moon'
                        })
                    }
                }
            })
            return false;
        });
    });
</script>